package io;

import java.io.*;
import java.nio.charset.StandardCharsets;

/**
 * 单字节写：
 * 单字节读：
 * 单字节文件复制：慢
 * 块读写的文件复制操作：快
 * 写文本数据：
 * 读取文本数据：
 */
public class IOTest01 {
    public static void main(String[] args) throws IOException {
        //单字节写
/*        File file = new File("fos1.dat");
        FileOutputStream fos1 = new FileOutputStream(file);
        fos1.write(12);
        fos1.write(100);
        fos1.write(1);
        System.out.println("写出完毕！");
        fos1.close();

        //单字节读
        FileInputStream fis1 = new FileInputStream("fos1.dat");
        int d = fis1.read();
        System.out.println(d);

        d = fis1.read();
        System.out.println(d);

        d = fis1.read();
        System.out.println(d);

        d = fis1.read();
        System.out.println(d);
        fis1.close();*/

/*        //单字节文件复制：慢
        FileInputStream fis1 = new FileInputStream("111.jpg");
        FileOutputStream fos1 = new FileOutputStream("111cp1.jpg");
        long start = System.currentTimeMillis();
        int d;
        while ((d = fis1.read())!=-1){
            fos1.write(d);
        }
        long end = System.currentTimeMillis();
        System.out.println("复制完毕！耗时:"+(end-start)+"ms");
        fis1.close();
        fos1.close();*/

/*        //块读写的文件复制操作：快
        FileInputStream fis1 = new FileInputStream("111.jpg");
        FileOutputStream fos1 = new FileOutputStream("111cp11.jpg");
        long start = System.currentTimeMillis();
        byte[] data = new byte[1024*10];
        int len ;
        while ((len = fis1.read(data))!=-1){
            fos1.write(data,0,len);
        }
        long end = System.currentTimeMillis();
        System.out.println("复制完毕!耗时:"+(end-start)+"ms");
        fis1.close();
        fos1.close();*/

/*        //写文本数据:
        FileOutputStream fos1 = new FileOutputStream("fos1.txt");
        fos1.write("IO流的第一次练习".getBytes(StandardCharsets.UTF_8));
        String name = ",写文本数据的另一种写法";
        byte[] data = name.getBytes(StandardCharsets.UTF_8);
        fos1.write(data);
        System.out.println("写入完毕");
        fos1.close();*/

        //读取文本数据:
        File file = new File("fos1.txt");
        FileInputStream fis1 = new FileInputStream(file);
        byte[] data = new byte[(int)file.length()];
        fis1.read(data);
        String line = new String(data,StandardCharsets.UTF_8);
        System.out.println(line);
        fis1.close();
    }
}
